/**
 * 实现Compose函数： 让多层包含调用改为单层一次性传参
 */

// https://juejin.cn/post/6968713283884974088#heading-1
// 用法如下:
function fn1(x) {
  return x + 1;
}
function fn2(x) {
  return x + 2;
}
function fn3(x) {
  return x + 3;
}
function fn4(x) {
  return x + 4;
}
const a = compose(fn1, fn2, fn3, fn4); //等于fn1( fn2( fn3( fn4(1) ) ) )
console.log( a(1) ); // 1+4+3+2+1=11

function compose(...callbacks) {
  var initFunc = (it) => it
  return callbacks.reduce((resultFn, item) => {
    var resultFnNew = (...args) => { // 形参, 返回构建的新函数( 欧，我只记得用来算数组结果，还能用来循环变换当前返回结果 )
      return resultFn(item(...args))
    }

    return resultFnNew
  }, initFunc)
}

